# -*- coding: utf-8 -*-
# @Time    : 2019/5/28 8:38
# @Author  : DrMa
from tqdm import tqdm,trange
import matplotlib.pyplot as plt
import numpy as np


def count_multi_charge(file_name_list):
    zm_count={}
    for name in file_name_list:
        f=open(name,'r+',encoding='utf-8')
        lines=f.readlines()
        for line in tqdm(lines):
            zm=line.split('    ')[1]
            if not zm in zm_count:
                zm_count[zm]=1
            else:
                zm_count[zm]+=1
        f.close()
    return zm_count


def count_one_charge(file_name_list):
    #输入是data, 拿到单项罪名的频次统计
    zm_count = {}
    for name in file_name_list:
        f = open(name, 'r+', encoding='utf-8')
        lines = f.readlines()
        for line in tqdm(lines):
            zms = line.split('    ')[1].strip('\n').split(' ')
            for zm in zms:
                if not zm in zm_count:
                    zm_count[zm] = 1
                else:
                    zm_count[zm] += 1
        f.close()
    return zm_count

def show_count(dic_count, num_of_x_value, max_count, step_count, name=None):

    plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
    plt.figure(figsize=(16, 16), dpi=80)
    plt.subplot(1, 1, 1)#两行一列,第一幅图,为了显示完全
    zm_count_order=list(dic_count.items())#字典转换为list, 用items()
    zm_count_order.sort(key=lambda s:s[1], reverse=True)
    print(zm_count_order[:-80:-1])
    #拿到样本值
    values = np.asarray(list(x[1] for x in zm_count_order))[:num_of_x_value]#把纵坐标的值放入
    x_names = np.asarray(list(x[0] for x in zm_count_order))[:num_of_x_value]  # 横坐标的文本放入
    index=np.arange(num_of_x_value)+1#横坐标数组,长度对应好
    #柱状图核心代码
    plt.bar(index, values, width=0.5, label="count", color="#87CEFA")#柱状图显示
    #横纵坐标标题
    plt.xlabel(name, fontsize=19)#横坐标标题
    plt.ylabel('Count of {0}'.format(name), fontsize=19)#纵坐标标题
    plt.title('Count for per_{0}'.format(name), fontsize=19)#总标题
    #横纵坐标赋值
    plt.xticks(index, x_names, rotation=90)#把横坐标的文本对应idnex显示. rotation可以旋转x轴坐标文本
    plt.yticks(np.arange(0, max_count, step_count))#显示y轴刻度和间隔
    #没啥用
    plt.legend(loc="upper right")
    #显示
    plt.show()

def descend_order_count(dic_count,end_num):
    #把罪名统计的字典输入, 我们根据频次倒序输出频次list
    #end_num代表我们倒序输出的个数
    zm_count_order = list(dic_count.items())  # 字典转换为list, 用items()
    zm_count_order.sort(key=lambda s: s[1], reverse=True)
    print(zm_count_order[:-end_num:-1])
# print(zm_count)

zm_count=count_multi_charge(['train','test','valid'])
zm_count_one=count_one_charge(['train','test','valid'])
# show_count(zm_count,'横坐标罪名',100,4000,100)
# show_count(zm_count_one,199,2000,200)
# descend_order_count(zm_count_one,100)

def filter_data(file, new_file, zm_count, min_num):
    '''
    :param file: 未筛选data
    :param new_file: 将筛选data地址
    :param zm_count: 单项罪名频次统计字典
    :param min_num:  最小值
    :return: 不返回, 只保存到本地
    '''
    zm_count_list=list(zm_count.items())
    filter_zm_dict=dict([x for x in zm_count_list if x[1]<=min_num])#
    #做个小实验, 看看train能不能包含所有罪名

    f1=open(file, 'r+', encoding='utf-8')
    f2=open(new_file, 'w+', encoding='utf-8')
    lines=f1.readlines()
    for line in tqdm(lines):
        zms=line.split('    ')[1].strip('\n').split()
        filter_or_not=True
        for zm in zms:
            if zm in filter_zm_dict:
                filter_or_not=False
                break
        if filter_or_not==True:
            f2.write(line)
    f1.close()
    f2.close()
# filter_data('valid','new_data/valid', zm_count_one, 50)
# filter_data('train','new_data/train', zm_count_one, 50)
# filter_data('test','new_data/test', zm_count_one, 50)

